var http = require('../../utils/http.js')
var app = getApp()
var that
Page({
  data: {
    cat_head_picurl: "../../images/MM.jpg",
    code:"",
    friend:'',
  },
  onLoad: function (options) {
    that = this
    that.setData({ code: options.code, friend:options.f})

    this.getUserInfo(function (res) {
      // console.log(app.globalData.userInfo);
    }, function (res) {
     that.onLoad();
    });
    var session_id = wx.getStorageSync('PHPSESSID');//本地取存储的sessionID
    if (session_id != "" && session_id != null) {
      wx.removeStorageSync('PHPSESSID')
    }
  },
  getUserInfo: function (cb, cp) {
    if (app.globalData.userInfo && wx.getStorageSync('outtime') > parseInt(Date.now() / 1000)) {
      typeof cb == "function" && cb(app.globalData.userInfo)
    } else {
      //调用登录接口
      wx.checkSession({
        success: function () {
          //session 未过期，并且在本生命周期一直有效
          wx.login({
            success: function (res) {
              if (res.code) {
                //发起网络请求
                http.check_member(res.code, app, cb, cp)
              } else {
                // console.log('获取用户登录态失败！' + res.errMsg)
              }
            }
          })
        },
        fail: function () {
          //登录态过期
          //重新登录
          wx.login({
            success: function (res) {
              if (res.code) {
                http.check_member(res.code, app, cb, cp)
              } else {
                http.toast('获取用户登录态失败！' + res.errMsg)
              }
            }
          })
        }
      })
    }
  },
  onReady: function () {
  
  },
  onShow: function () {
    
  },
  invt:function(){
    app.req("get_level.html", { "openid": app.data.openid}, function (res) {
      if (res.status == "success") {
       if(res.mess.level>0){
        app.toast_up("您已是会员！")
       } else {wx.redirectTo({ url: './invite_code?code=' + that.data.code, })}
      } else if (res.status == "error") {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.invt()
          }
        })
      }
    })
  }
})